package com.yue.pattern.singleton;

import java.io.Serializable;

/**
 * 序列化与反序列化保证单例
 */
public class Seriable implements Serializable {

    public final static Seriable INSTANCE = new Seriable();

    private Seriable() {
    }

    public static Seriable getInstance() {
        return INSTANCE;
    }

    private Object readResolve(){
        return INSTANCE;
    }
}

/**
 * Seriable s1 = null;
 * Seriable s2 = Seriable.getInstance();
 * FileOUtputStream fos = new FileOutpuStream("Seriablee.obj");
 * ObjectOutputStream oos = new ObjectOutpuStream(fos);
 * oos.writeObject(s2);
 * oos.flush();
 * oos.close();
 *
 *
 * FileInputStream fis = new FileInputStream("Seriable.obj");
 * ObjectInputStream ois = new ObjectInputStream(fis);
 * s1 = (Seriabale)ois.readObject();
 * ois.close();
 *
 * 结果  s1 == s2  为 false
 *
 * 解决方法： Seriable 类中 重写 readresolve(); 这是序列化与反序列化中的一个协议，此方法由JVM 调用
 * private Object readResolve(){
 *     return INSTANCE:
 * }
 *
 */
